<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="~assets/img/login_portrait.jpg" alt />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">out</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边 -->
      <el-aside :width="IsAside ? '64px': '200px'">
        <div class="aside_collapse" @click="isCollapse">///</div>
        <el-menu
          background-color="#292929"
          text-color="#fff"
          active-text-color="#1C86EE"
          :unique-opened="true"
          :collapse="IsAside"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级 -->
          <el-submenu :index="item.id + ''" v-for="item in AsideList" :key="item.id">
            <!-- 一级区域 -->
            <template slot="title">
              <!-- 一级图标 -->
              <i :class="AsideIcon[item.id]"></i>
              <!-- 一级文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveNavState('/' + subitem.path)"
            >
              <!-- 二级图标 -->
              <i class="el-icon-s-grid"></i>
              <!-- 二级文本 -->
              <span>{{subitem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 侧边数据
      AsideList: [],
      AsideIcon: {
        125: 'iconfont icon-yonghu',
        103: 'iconfont icon-shangpingouwudai2',
        101: 'iconfont icon-shuju',
        102: 'iconfont icon-dingdan_huaban',
        145: 'iconfont icon-quanxian'
      },
      // 侧边是否折叠
      IsAside: false,
      // 导航高亮index
      activePath: ''
    }
  },
  created() {
    this.getAsideData()
    this.activePath = window.sessionStorage.getItem('savePath')
  },
  methods: {
    // 退出
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边数据
    async getAsideData() {
      // data解构出来，重命名为res
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.AsideList = res.data
      // console.log(this.AsideList)
    },
    // 折叠
    isCollapse() {
      this.IsAside = !this.IsAside
    },
    // 点击高亮获取index设置default-active
    saveNavState(index) {
      window.sessionStorage.setItem('savePath', index)
      this.activePath = index
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #242424;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  font-size: 20px;
  > div {
    display: flex;
    color: #fff;
    align-items: center;
    span {
      margin-left: 9px;
    }
    img {
      width: 30px;
      height: 30px;
    }
  }
}
.el-aside {
  background-color: #292929;
  .aside_collapse {
    text-align: center;
    color: #fff;
  }
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #fff;
}
</style>
